package cn.hjh.service;

import cn.hjh.dao.HibernateUtil;
import cn.hjh.dao.SpareDao;
import cn.hjh.pojo.Spare;
import org.hibernate.HibernateException;
import org.hibernate.Transaction;

import java.util.List;

public class SpareBiz {
    SpareDao spareDao = new SpareDao();

    /* 第5章简答题 */
    public List<Spare> findSpareBysparemodel(String sparemodel){
        Transaction tx = null;
        List<Spare> result = null;
        try {
            tx = HibernateUtil.currentSession().beginTransaction();
            result = spareDao.findSpareBysparemodel(sparemodel);
            tx.commit();
        } catch (HibernateException e) {
            e.printStackTrace();
        }
        return result;
    }

    //查询出厂价格小于指定价格的备件
    public List<Spare> findSpareByprice(){
        Transaction tx = null;
        List<Spare> spares = null;
        try {
            tx = HibernateUtil.currentSession().beginTransaction();
            spares = spareDao.findSpareByprice();
            tx.commit();
        } catch (HibernateException e) {
            e.printStackTrace();
            if (tx != null)
                tx.rollback();
        }
        return spares;
    }

    //查询出厂日期小于指定日期的备件
    public List<Spare> findSpareByriqi(){
        Transaction tx = null;
        List<Spare> spares = null;
        try {
            tx = HibernateUtil.currentSession().beginTransaction();
            spares = spareDao.findSpareByriqi();
            tx.commit();
        } catch (HibernateException e) {
            e.printStackTrace();
            if (tx != null)
                tx.rollback();
        }
        return spares;
    }

    public List<Spare> finSpareBysan(Spare conditions){
        Transaction tx = null;
        List<Spare> spareList = null;
        try {
            tx = HibernateUtil.currentSession().beginTransaction();
            spareList = spareDao.finSpareBysan(conditions);
            tx.commit();
        } catch (HibernateException e) {
            e.printStackTrace();
            if (tx != null)
                tx.rollback();
        }
        return spareList;
    }

    public void save(Spare spare){
        Transaction tx =null;

        try {
            tx=HibernateUtil.currentSession().beginTransaction();
            spareDao.save(spare);
            tx.commit();
        } catch (HibernateException e) {
            e.printStackTrace();
            if(tx!=null)
                tx.rollback();
        }
    }

    /*修改备件信息*/
    public void updateSpare(Spare spare){
        Transaction tx = null;
        try {
            tx = HibernateUtil.currentSession().beginTransaction(); //开启事物
            Spare spareToUpdate = spareDao.get(spare.getId());
            spareToUpdate.setSparemodel(spare.getSparemodel());
            spareToUpdate.setPrice(spare.getPrice());
            spareToUpdate.setProduction(spare.getProduction());
            tx.commit();    //关闭事物
        } catch (HibernateException e) {
            e.printStackTrace();
            if (tx != null)
                tx.rollback();
        }
    }

    public void delete(Integer id) {
        Transaction tx = null;
        try {
            tx = HibernateUtil.currentSession().beginTransaction();
            Spare spare = spareDao.get(id);
            spareDao.delete(spare);
            tx.commit();
        } catch (HibernateException e) {
            e.printStackTrace();
            if (tx != null) {
                tx.rollback();
            }
        }
    }
}
